<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" />
      </div>
      <el-form :model="form" :rules="rules" ref="form" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="tc">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from "@/api/login";
import { Message } from "element-ui";

export default {
  name: "loginIndex",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "请输入3-10个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            pattern: /^\w{6,15}$/,
            message: "请输入6-15位密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async login() {
      await this.$refs.form.validate();
      try {
        const res = await loginApi(this.form);
        // console.log(res);
        this.$store.commit("user/setToken", res.data.token);
        Message.success("登录成功");
        this.$router.push("/");
      } catch (err) {
        this.$message.error("账户或密码错误");
        console.log(err);
      }
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: #2b4b6b;
  .login-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    .avatar-box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
        overflow-clip-margin: content-box;
        overflow: clip;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .tc {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
